package com.sec.android.app.myfiles.module.optimizestorage;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.cloud.CloudFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.StorageMonitor;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

import java.util.List;
import java.util.Locale;

/**
 * Created by daesu83.kim on 2016-10-07.
 */

public class OptimizeStorageAdapterImp extends AbsFileListAdapterImp {

    public enum FilterType {
        ALL,
        VIDEO_ONLY,
        IMAGE_ONLY,
        AUDIO_ONLY,
        DOCUMENT_ONLY,
        CONTENTS_TYPE_OTHERS,
        INTERNAL_STORAGE,
        SD_CARD,
        CLOUD,
        NONE
    }

    private FileRecord.OptimizeStorageType mOptimizeStorageType = FileRecord.OptimizeStorageType.None;

    private List<String> mGroupNames;
    private long[] mGroupFileSizes;
    private boolean mNeedRefresh = true;

    private int mIdIndex;
    private int mSrcDbIdIndex;
    private int mStorageTypeIndex;
    private int mPathIndex;
    private int mSizeIndex;
    private int mDateIndex;
    private int mFileTypeIndex;
    private int mFileIdIndex;
    private int mParentIdIndex;
    private int mDeviceIdIndex;

    private FilterType mCurrentFilter = FilterType.ALL;
    private TextView mLogFilesCount;
    private TextView mTemporaryFilesCount;
    private TextView mEmptyFilesCount;


    public OptimizeStorageAdapterImp(Context context, NavigationInfo navigationInfo, AbsListViewImp listView, Cursor c, int flags) {
        super(context, navigationInfo, listView, c, flags);
        ensureOptimizeStorageType(navigationInfo);
        initColumnIndex();
    }

    public OptimizeStorageAdapterImp(Context context, NavigationInfo navigationInfo, int layoutId, Cursor c, int flags) {
        super(context, navigationInfo, layoutId, c, flags);
        ensureOptimizeStorageType(navigationInfo);
        initColumnIndex();
    }

    private void initColumnIndex() {
        mIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ID);
        mSrcDbIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.MEDIA_ID);
        mStorageTypeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.STORAGE_TYPE);
        mPathIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH);
        mSizeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.SIZE);
        mDateIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.DATE);
        mFileTypeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.FILE_TYPE);
        mFileIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.FILE_ID);
        mDeviceIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.DEVICE_ID);
        mParentIdIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PARENT_CLOUD_ID);
    }

    private void ensureOptimizeStorageType(NavigationInfo info) {
        if (info != null) {
            FileRecord curRecord = info.getCurRecord();
            if (curRecord instanceof OptimizeStorageFileRecord) {
                mOptimizeStorageType = ((OptimizeStorageFileRecord) curRecord).getOptimizeStorageType();
            }
        }
    }

    @Override
    public Uri getUri() {
        Uri.Builder builder = super.getUri().buildUpon();
        builder.appendQueryParameter(OptimizeStorageDbTableInfo.QUERY_PARAM_OPTIMIZE_STORAGE_TYPE,
                mOptimizeStorageType.toString())
                .appendQueryParameter(OptimizeStorageDbTableInfo.QUERY_PARAM_REFRESH,
                        mNeedRefresh ? "true" : "false")
                .appendQueryParameter(OptimizeStorageDbTableInfo.QUERY_PARAM_IS_FILTER_ALL,
                        mCurrentFilter == FilterType.ALL ? "true" : "false");
        mNeedRefresh = false;
        return builder.build();
    }

    @Override
    public String getSelection() {
        String selection = null;
        switch (mCurrentFilter) {
            case VIDEO_ONLY:
                selection = getFileTypeRangeSelection(FileType.getVideoFileTypeRange());
                break;

            case IMAGE_ONLY:
                selection = getFileTypeRangeSelection(FileType.getImageFileTypeRange());
                break;

            case AUDIO_ONLY:
                selection = getFileTypeRangeSelection(FileType.getAudioFileTypeRange());
                break;

            case DOCUMENT_ONLY:
                selection = getFileTypeRangeSelection(FileType.getDocumentFileTypeRange());
                break;

            case CONTENTS_TYPE_OTHERS:
                selection = String.format(Locale.getDefault(), "(not %s) and (not %s) and (not %s) and (not %s)",
                        getFileTypeRangeSelection(FileType.getVideoFileTypeRange()),
                        getFileTypeRangeSelection(FileType.getImageFileTypeRange()),
                        getFileTypeRangeSelection(FileType.getAudioFileTypeRange()),
                        getFileTypeRangeSelection(FileType.getDocumentFileTypeRange()));
                break;

            case INTERNAL_STORAGE:
            case SD_CARD:
                selection = mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH) + " LIKE ?";
                break;

            case CLOUD:
                selection = mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.STORAGE_TYPE) +
                        "='" + FileRecord.StorageType.Cloud.toString() + "'";
                break;
        }
        return selection;
    }

    private String getFileTypeRangeSelection(int[] fileTypeRange) {
        String fileTypeColumnName = mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.FILE_TYPE);
        return fileTypeColumnName+ " between "+fileTypeRange[0]+" and "+fileTypeRange[1];
    }

    @Override
    public String[] getSelectionArgs() {
        String[] ret = null;
        switch (mCurrentFilter) {
            case INTERNAL_STORAGE:
                ret = new String[]{
                        StorageMonitor.getStoragePath(mContext, StorageMonitor.iStorageType.INTERNAL) + "/%"};
                break;

            case SD_CARD:
                ret = new String[]{
                        StorageMonitor.getStoragePath(mContext, StorageMonitor.iStorageType.EXTERNAL_SD) + "/%"};
                break;
        }
        return ret;
    }

    @Override
    public String getOrderBy() {
        String ret;
        switch (mOptimizeStorageType) {
            case LargeFiles:
                ret = mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.SIZE) + " desc";
                break;

            case UnusedFiles:
                ret = mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DATE) + " asc";
                break;

            default:
                ret = null;
                break;
        }
        return ret;
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        FileRecord ret = null;
        if (cursor != null) {
            try {
                int id = cursor.getInt(mIdIndex);
                int srcDbId = cursor.getInt(mSrcDbIdIndex);
                String strStorageType = cursor.getString(mStorageTypeIndex);
                String full_path = cursor.getString(mPathIndex);
                String path = FileRecord.getPath(full_path);
                String name = FileRecord.getName(full_path);
                long size = cursor.getLong(mSizeIndex);
                long date = cursor.getLong(mDateIndex) * 1000;
                int fileType = cursor.getInt(mFileTypeIndex);
                String fileId = cursor.getString(mFileIdIndex);
                String parentId = cursor.getString(mParentIdIndex);
                int isHidden = 0;
                int itemCount = 0;
                int itemCountHidden = 0;

                FileRecord.StorageType storageType = FileRecord.StorageType.valueOf(strStorageType);
                if (storageType == FileRecord.StorageType.Cloud) {
                    int deviceId = cursor.getInt(mDeviceIdIndex);
                    path = makeCloudPath(deviceId, path);
                    CloudFileRecord cloudFileRecord = new CloudFileRecord(srcDbId, deviceId, fileId, path, name, size, date, MediaFile.getFileType(name), 0);
                    cloudFileRecord.setParentId(parentId);
                    ret = cloudFileRecord;
                } else {
                    ret = FileRecord.createFileRecord(FileRecord.StorageType.Local, srcDbId, path, name,
                            size, date, fileType, isHidden, itemCount, itemCountHidden);
                }
                ret.setCreatedFrom(FileRecord.StorageType.OptimizeStorage);
            } catch (IllegalStateException e) {
                Log.e(this, "IllegalStateException:" + e.toString());
            }
        }
        return ret;
    }

    private String makeCloudPath(int deviceId, String pathInCloud) {
        String cloudRoot;
        if (CloudMgr.getInstance(mContext).getCloudType(deviceId) == FileRecord.CloudType.GoogleDrive) {
            cloudRoot = AppConstants.StoragePath.GOOGLE_DRIVE_FOLDER;
        } else {
            cloudRoot = AppConstants.StoragePath.SAMSUNG_DRIVE_FOLDER;
        }
        if (TextUtils.isEmpty(pathInCloud)) {
            return cloudRoot;
        } else {
            return cloudRoot + pathInCloud;
        }
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return getFilePath(cursor, mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH));
    }

    @Override
    public FileRecord.StorageType getStorageType() {
        return FileRecord.StorageType.OptimizeStorage;
    }

    @Override
    public long getItemId(int position) {
        long itemId = super.getItemId(position);
        if (isGroupHeader(position)) {
            int groupPosition = getGroupPosition(position);
            String groupName = mGroupNames.get(groupPosition);
            if (mOptimizeStorageType == FileRecord.OptimizeStorageType.DuplicateFiles &&
                    mGroupFileSizes != null) {
                itemId = (groupName + mGroupFileSizes[groupPosition]).hashCode();
            } else {
                itemId = groupName.hashCode();
            }
        }
        return itemId;
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        View view = super.newView(context, c, parent);
        if (mOptimizeStorageType == FileRecord.OptimizeStorageType.UnnecessaryData) {
            View thumbnailLayout = view.findViewById(R.id.thumbnail_layout);
            if (thumbnailLayout != null) {
                Resources resources = mContext.getResources();
                int size = resources.getDimensionPixelSize(R.dimen.list_item_column_file_type_icon_size);
                thumbnailLayout.getLayoutParams().width = size;
                thumbnailLayout.getLayoutParams().height = size;
            }
        }
        return view;
    }

    @Override
    protected ListViewHolder _createViewHolder(View v) {
        ListViewHolder vh = super._createViewHolder(v);
        View thumbnailLayout = v.findViewById(R.id.thumbnail_layout);
        vh.addView(FileListViewHolder.THUMBNAIL_LAYOUT, thumbnailLayout);
        if (thumbnailLayout != null) {
            thumbnailLayout.setOnClickListener(mThumbnailClickListener);
        }

        View divider = v.findViewById(R.id.divider);
        vh.addView(FileListViewHolder.DIVIDER, divider);

        if (mOptimizeStorageType == FileRecord.OptimizeStorageType.DuplicateFiles) {
            View iconView = v.findViewById(R.id.storage_icon);
            if (iconView != null) {
                iconView.setVisibility(View.VISIBLE);
                vh.addView(FileListViewHolder.STORAGE_ICON, iconView);
            }

            TextView textName = vh.getView(FileListViewHolder.NAME, TextView.class);
            if (textName != null) {
                int textSize = mContext.getResources().getDimensionPixelSize(R.dimen.list_item_text2_size);
                textName.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                textName.setEllipsize(TextUtils.TruncateAt.END);
                textName.setMaxLines(2);
            }
        }
        return vh;
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        super._bindView(context, vh, v, record, position);
        if (mGroupNames != null) {
            int visibility = isLastItemOfGroup(position) ? View.GONE : View.VISIBLE;
            View divider = vh.getView(FileListViewHolder.DIVIDER);
            if (divider != null && divider.getVisibility() != visibility) {
                divider.setVisibility(visibility);
            }
        }
    }

    @Override
    protected void _bindDefaultInfoView(Context context, ListViewHolder vh, FileRecord record) {
        String firstLineText = null;
        String secondLineText = null;
        switch (mOptimizeStorageType) {
            case LargeFiles:
                firstLineText = record.getName();
                secondLineText = UiUtils.makeFileSizeString(mContext, record.getSize());
                break;

            case UnusedFiles:
                firstLineText = record.getName();
                secondLineText = UiUtils.makeTimeString(mContext, record.getDate());
                break;

            case DuplicateFiles:
                firstLineText = getUserFriendlyPath(record);
                secondLineText = UiUtils.makeTimeString(mContext, record.getDate());

                ImageView storageIcon = vh.getView(FileListViewHolder.STORAGE_ICON, ImageView.class);
                if (storageIcon != null) {
                    setStorageIcon(storageIcon, record);
                }
                break;

            case UnnecessaryData:
                firstLineText = record.getName();
                secondLineText = getUserFriendlyPath(record);
                break;
        }

        TextView textName = vh.getView(FileListViewHolder.NAME, TextView.class);
        if (textName != null) {
            textName.setText(firstLineText);
            textName.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        }

        TextView description = vh.getView(FileListViewHolder.DESCRIPTION, TextView.class);
        if (description != null) {
            description.setText(secondLineText);
        }
    }

    private String getUserFriendlyPath(FileRecord record) {
        String ret = "";
        switch (record.getStorageType()) {
            case Local:
                ret = StorageMonitor.convertPathToDisplayedPath(mContext, record.getPath());
                break;

            case Cloud:
                ret = record.getPath();
                break;
        }
        return ret;
    }

    private void setStorageIcon(ImageView iconView, FileRecord record) {
        if (iconView != null) {
            int iconResId = -1;
            int tintColorId = -1;
            final FileRecord.StorageType storageType = record.getStorageType();
            switch (storageType) {
                case Local:
                    String fullPath = record.getFullPath();
                    int localStorageType = StorageMonitor.getMatchedStorageType(fullPath);
                    if (localStorageType == StorageMonitor.iStorageType.INTERNAL) {
                        iconResId = R.drawable.my_files_ic_phonestorage;
                        tintColorId = R.color.home_icon_device_storage_color;
                    } else if (localStorageType == StorageMonitor.iStorageType.EXTERNAL_SD) {
                        iconResId = R.drawable.my_files_ic_storage;
                        tintColorId = R.color.home_icon_sd_card_color;
                    } else if (StorageMonitor.isUsbRoot(fullPath)) {
                        iconResId = R.drawable.my_files_ic_usb;
                        tintColorId = R.color.home_icon_usb_storage_color;
                    }
                    break;

                case Cloud:
                    iconResId = R.drawable.my_files_ic_cloud;
                    tintColorId = R.color.home_icon_samsung_drive_color;
                    if (CloudMgr.getInstance(mContext).getCloudType(((CloudFileRecord) record).getDeviceId()) ==
                            FileRecord.CloudType.GoogleDrive) {
                        iconResId = R.drawable.my_files_ic_drive;
                        tintColorId = R.color.home_icon_google_drive_color;
                    }
                    break;
            }
            if (iconResId != -1) {
                iconView.setImageResource(iconResId);
                iconView.setColorFilter(ContextCompat.getColor(mContext, tintColorId));
            }
        }
    }

    @Override
    protected boolean _bindThumbnailView(Context context, ListViewHolder vh, View v, FileRecord record) {
        boolean ret = super._bindThumbnailView(context, vh, v, record);
        View thumbnailLayout = vh.getView(FileListViewHolder.THUMBNAIL_LAYOUT);
        if (thumbnailLayout != null) {
            thumbnailLayout.setTag(record);
        }
        return ret;
    }

    @Override
    public void changeCursor(Cursor cursor) {
        if (cursor != null) {
            if (cursor.getColumnIndex(BaseColumns._ID) == -1) {
                Log.e(this, "Cursor is invalid - there is no column(_id)");
                cursor = null;
            } else {
                Bundle extras = cursor.getExtras();
                int[] groupSizes = extras.getIntArray(OptimizeStorageDbTableInfo.EXTRAS_KEY_GROUP_CHILD_COUNT);
                mGroupNames = extras.getStringArrayList(OptimizeStorageDbTableInfo.EXTRAS_KEY_GROUP_NAME);
                mGroupFileSizes = extras.getLongArray(OptimizeStorageDbTableInfo.EXTRAS_KEY_DUPLICATED_FILES_FILE_SIZE);
                setGroupInfo(groupSizes);
            }
        }

        if (cursor == null) {
            Log.e(this, "changeCursor - cursor is null");
            setGroupInfo(null);
            mGroupNames = null;
        }
        super.changeCursor(cursor);
    }

    @Override
    protected View _newGroupHeaderView(Context context, ListViewHolder vh, int groupPosition) {
        View view;
        ViewGroup groupHeaderContainer = vh.getView(FileListViewHolder.GROUP_HEADER, ViewGroup.class);
        LayoutInflater inflater = LayoutInflater.from(context);
        if (mOptimizeStorageType == FileRecord.OptimizeStorageType.DuplicateFiles) {
            view = inflater.inflate(R.layout.optimize_storage_file_list_group_header,
                    groupHeaderContainer, false);
        } else {
            view = inflater.inflate(R.layout.optimize_storage_file_list_section_header,
                    groupHeaderContainer, false);
        }
        vh.addView(FileListViewHolder.GROUP_HEADER_TITLE, view.findViewById(R.id.title));
        vh.addView(FileListViewHolder.GROUP_HEADER_ITEM_COUNT, view.findViewById(R.id.item_count));
        return view;
    }

    @Override
    protected void _bindGroupHeaderView(Context context, ListViewHolder vh, View v, int groupPosition) {
        if (mOptimizeStorageType == FileRecord.OptimizeStorageType.UnnecessaryData) {
            View groupHeaderContainer = vh.getView(FileListViewHolder.GROUP_HEADER);
            if (groupHeaderContainer != null) {
                ViewGroup vg = (ViewGroup) groupHeaderContainer.findViewById(R.id.group_header_layout);
                if (vg != null) {
                    if (groupPosition == 0) {
                        View infoPanel = (View) vg.getTag();
                        if (infoPanel == null) {
                            infoPanel = LayoutInflater.from(context)
                                    .inflate(R.layout.optimize_storage_unnecessary_files_info, vg, false);
                            vg.setTag(infoPanel);
                            ensureUnnecessaryFilesInfoPanel(infoPanel);
                            updateUnnecessaryFilesInfo();
                        }
                        vg.addView(infoPanel, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));
                    } else {
                        if (vg.getChildCount() > 1) {
                            vg.removeViewAt(0);
                        }
                    }
                }
            }
        }

        View divider = v.findViewById(R.id.top_divider);
        if (divider != null) {
            divider.setVisibility(groupPosition == 0 ? View.GONE : View.VISIBLE);
        }

        if (mGroupNames != null) {
            TextView titleTextView = vh.getView(FileListViewHolder.GROUP_HEADER_TITLE, TextView.class);
            if (titleTextView != null) {
                titleTextView.setText(mGroupNames.get(groupPosition));
            }
        }

        TextView itemCountTextView = vh.getView(FileListViewHolder.GROUP_HEADER_ITEM_COUNT, TextView.class);
        if (itemCountTextView != null) {
            int childCount = getGroupItemCount(groupPosition);
            itemCountTextView.setText(String.format("(%s)", UiUtils.makeItemsString(mContext, childCount)));
        }
    }

    private void ensureUnnecessaryFilesInfoPanel(View panel) {
        if (panel != null) {
            TextView unnecessaryFilesInfo = (TextView) panel.findViewById(R.id.unnecessary_files_info_text);
            if (unnecessaryFilesInfo != null) {
                unnecessaryFilesInfo.setText(AppFeatures.isTablet() ?
                        mContext.getText(R.string.unnecessary_files_info_panel_text_tablet) :
                        mContext.getText(R.string.unnecessary_files_info_panel_text_phone));
            }
            mLogFilesCount = (TextView) panel.findViewById(R.id.log_files);
            mTemporaryFilesCount = (TextView) panel.findViewById(R.id.temporary_files);
            mEmptyFilesCount = (TextView) panel.findViewById(R.id.empty_files);
            setResultFileCount(0, 0, 0);
        }
    }

    private void updateUnnecessaryFilesInfo() {
        Cursor cursor = getCursor();
        int logFilesCount = 0;
        int tempFilesCount = 0;
        int emptyFilesCount = 0;
        if (cursor != null) {
            Bundle extras = cursor.getExtras();
            List<String> groupNames = extras.getStringArrayList(OptimizeStorageDbTableInfo.EXTRAS_KEY_GROUP_NAME);
            int[] groupSizes = extras.getIntArray(OptimizeStorageDbTableInfo.EXTRAS_KEY_GROUP_CHILD_COUNT);
            if (groupNames != null && groupSizes != null) {
                String logFiles = mContext.getString(R.string.log_files);
                String tempFiles = mContext.getString(R.string.temporary_files);
                String emptyFiles = mContext.getString(R.string.empty_files);
                for (int i = 0; i < groupNames.size(); i++) {
                    String groupName = groupNames.get(i);
                    if (groupName.equals(logFiles)) {
                        logFilesCount = groupSizes[i];
                    } else if (groupName.equals(tempFiles)) {
                        tempFilesCount = groupSizes[i];
                    } else if (groupName.equals(emptyFiles)) {
                        emptyFilesCount = groupSizes[i];
                    }
                }
            }
        }
        setResultFileCount(logFilesCount, tempFilesCount, emptyFilesCount);
    }

    private void setResultFileCount(int logFiles, int temporaryFiles, int emptyFiles) {
        if (mLogFilesCount != null) {
            mLogFilesCount.setText(mContext.getString(R.string.log_files_pd, logFiles));
        }
        if (mTemporaryFilesCount != null) {
            mTemporaryFilesCount.setText(mContext.getString(R.string.temporary_files_pd, temporaryFiles));
        }
        if (mEmptyFilesCount != null) {
            mEmptyFilesCount.setText(mContext.getString(R.string.empty_files_pd, emptyFiles));
        }
    }

    public void setFilterType(FilterType filterType) {
        mCurrentFilter = filterType;
    }

    public FilterType getFilterType() {
        return mCurrentFilter;
    }

    public void needRefresh() {
        setNeedRefresh(true);
    }

    public void setNeedRefresh(boolean need) {
        mNeedRefresh = need;
    }

    private View.OnClickListener mThumbnailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mNavigationInfo != null) {
                AbsMyFilesFragment curFragment = mNavigationInfo.getCurFragment();
                if (curFragment != null) {
                    int processId = curFragment.getProcessId();
                    Object record = v.getTag();
                    if (record instanceof FileRecord) {
                        MyFilesFacade.executeRecord(processId, curFragment.getActivity(), (FileRecord) record);
                    }
                }
            }
        }
    };
}
